package com.zhuiYun.project.api.customer.service.impl;


import cn.hutool.json.JSONObject;
import com.zhuiYun.project.api.customer.entity.FriendEntity;
import com.zhuiYun.project.api.customer.entity.MessageEntity;
import com.zhuiYun.project.api.customer.entity.MsgDTO;
import com.zhuiYun.project.api.customer.mapper.FriendMapper;
import com.zhuiYun.project.api.customer.mapper.MessageMapper;
import com.zhuiYun.project.api.customer.service.CustomerService;
import com.zhuiYun.project.api.login.entity.User;
import com.zhuiYun.project.common.response.CommonConstants;
import com.zhuiYun.project.common.response.CommonResult;
import com.zhuiYun.project.util.PrimaryKeyUtil;
import com.zhuiYun.project.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * LC: 2023-04-03 14:11
 */
@Service
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    MessageMapper messageMapper;

    @Autowired
    FriendMapper friendMapper;

    @Autowired
    PrimaryKeyUtil primaryKeyUtil;




    /**
     * @descriptions 保存聊天记录
     * LC: 2023-04-04 13:50
     */
    @Override
    public void storeMessage(String userId, String friendId, String text) {
        Date date = new Date();
        // 查询会话列表
        FriendEntity friend = friendMapper.getFriend(Integer.parseInt(userId),Integer.parseInt(friendId));
        FriendEntity friend1 = friendMapper.getFriend(Integer.parseInt(friendId),Integer.parseInt(userId));
        // 未读数量
        Integer i = friend1.getUnread() + 1;
        Integer i1 = friend.getUnread() + 1;
        // 修改会话
        friendMapper.updateFriendByIds(friend.getSessionId(),friend.getMyId(),text,date,i);
        friendMapper.updateFriendByIds(friend.getSessionId(),friend.getFriendId(),text,date,i1);
        // 新增消息
        MessageEntity message1 = new MessageEntity();
        message1.setFriendId(friend.getSessionId());
        message1.setSenderId(Integer.parseInt(userId));
        message1.setReceiverId(Integer.parseInt(friendId));
        message1.setSendTime(date);
        message1.setContent(text);
        message1.setCreateTime(date);
        message1.setUpdateTime(date);
        message1.setDelFlag(0);
        messageMapper.insertUseGeneratedKeys(message1);
    }

    /**
     * 查询会话列表
     * LC: 2023-04-04 14:48
     * @param myId
     * @param friendId
     * @return
     */
    @Override
    public FriendEntity getFriend(Integer myId, Integer friendId) {
        return friendMapper.getFriend(myId,friendId);
    }

    /**
     * 查询聊天记录
     * LC: 2023-04-04 13:51
     */
    @Override
    public List<JSONObject> readMessage(Long sessionId) {
        return messageMapper.readMessage(sessionId);
    }

    /**
     * @descriptions 获取客服信息
     * LC: 2023-04-04 13:52
     * @param myId
     * @return result.CommonResult
     */
    @Override
    public List<JSONObject> getFriendList(Integer myId) {
        return friendMapper.getFriendList(myId);
    }

    /**
     * 清空读取信息
     * LC: 2023-04-04 13:53
     * @param myId
     * @param friendId
     * @return
     */
    @Override
    public void emptyUnRead(Integer myId, Integer friendId) {
        friendMapper.emptyUnRead(myId,friendId);
    }

    @Override
    public Integer saveFriend(Integer id) {
        // 查询会话
        List<FriendEntity> friendEntityList = friendMapper.getUserFriend(id);
        // 新增会话
        Integer i = 0;
        if(friendEntityList.size()==0){
            User userList = friendMapper.getUserList(id);
            FriendEntity friendEntity = new FriendEntity();
            FriendEntity friendEntity1 = new FriendEntity();
            String num = primaryKeyUtil.getOrderId();
            friendEntity.setSessionId(Long.valueOf(num));
            friendEntity.setMyId(id);
            friendEntity.setFriendId(2);
            friendEntity.setRemark("客服");
            friendEntity.setUnread(0);
            friendEntity.setCreateTime(new Date());
            friendEntity.setDelFlag(0);
            i = friendMapper.insertSelective(friendEntity);
            if(i>0){
                friendEntity1.setSessionId(friendEntity.getSessionId());
                friendEntity1.setMyId(2);
                friendEntity1.setFriendId(id);
                friendEntity1.setRemark(userList.getNickName());
                friendEntity1.setUnread(0);
                friendEntity1.setCreateTime(new Date());
                friendEntity1.setDelFlag(0);
                i = friendMapper.insertSelective(friendEntity1);
            }
        }
        return i;
    }

    /**
    * @Description 保存用户聊天记录
    * @Author lzy
    * @Date 2023/4/20 11:29
    * @Return com.zhuiYun.project.common.response.CommonResult
     * @param msgVos
     * @param userId
    */
    @Override
    public CommonResult saveChatRecord(List<MsgDTO> msgVos, String userId) {
        boolean set = RedisUtil.set(CommonConstants.USER_CHAT_RECORD + userId, msgVos);
        if (set) {
            return CommonResult.ok();
        } else {
            return CommonResult.error();
        }
    }

    /**
    * @Description 根据用户id获取用户聊天记录
    * @Author lzy
    * @Date 2023/4/20 11:36
    * @Return com.zhuiYun.project.common.response.CommonResult
    * @param userId
    */
    @Override
    public CommonResult getChatRecordByUserId(String userId) {
        List<MsgDTO> msgVos = (List<MsgDTO>) RedisUtil.get(CommonConstants.USER_CHAT_RECORD + userId);
        return CommonResult.ok(msgVos);
    }
}
